package com.example.myapplication2.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ViewPagerDemo extends PagerAdapter{
   private Context context;
   private ArrayList<HashMap<String, Object>> list;
   private List<ImageView> imageViewList=new ArrayList<>();
    public ViewPagerDemo(Context context, ArrayList<HashMap<String, Object>> list) {
        this.context=context;
        this.list=list;
        for (HashMap<String, Object> hashMap : list) {
            int ioc = (int) hashMap.get("ioc");
            ImageView imageView = new ImageView(context);
            imageView.setLayoutParams(
                    new ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT));
            imageView.setImageResource(ioc);
            imageViewList.add(imageView);
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }
    //比较两个对象是否相等,根据instantiateItem方法里面的返回值关系决定的
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    //初始化
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ImageView imageView = imageViewList.get(position);
        container.addView(imageView);
        return imageView;
    }

    //销毁
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(imageViewList.get(position));
    }
}
